import * as React from 'react';

import { StyleSheet, View, Text, Button } from 'react-native';
import WechatTs from 'react-native-wechat-ts';

WechatTs.registerApp('wx1234576900');
export default function App() {
  const [install, setInstall] = React.useState<boolean>(false);
  React.useEffect(() => {
    const initSDK = async () => {
      const isInstall = await WechatTs.isWXAppInstalled();
      setInstall(isInstall);
      console.log(`是否安装微信：${isInstall}`);
      console.log(`current version is: ` + (await WechatTs.getApiVersion()));
    };
    initSDK();
  }, []);

  const open = async () => {
    await WechatTs.openWXApp();
  };
  const auth = async () => {
    const res = await WechatTs.sendAuthRequest(
      'snsapi_userinfo',
      'wechat_sdk_demo_test'
    );

    console.log(res);
  };

  const pay = () => {
    WechatTs.pay({
      partnerId: '123',
      timeStamp: '123',
      packageValue: 'Sign=WXPay',
      sign: '14123',
      nonceStr: 'eqwe',
      prepayId: 'adasd',
    })
      .then((res) => {
        console.log(res);
      })
      .catch((err) => {
        console.log(err);
      });
  };

  return (
    <View style={styles.container}>
      <Text>微信是否安装：{install ? 'y' : 'n'}</Text>
      <Button title="打开微信" onPress={open} />
      <Sep />
      <Button title="微信登录" onPress={auth} />
      <Sep />
      <Button title="微信支付" onPress={pay} />
    </View>
  );
}
const Sep = () => <View style={styles.sep} />;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  box: {
    width: 60,
    height: 60,
    marginVertical: 20,
  },
  sep: {
    height: 20,
  },
});
